How one object can be built on another, what a child gets for free, why writing an event yourself replaces the parent's, and how event_call(parent) gives you both.
An object is a plan for one kind of thing in your game. obj_enemy is a plan for enemies. Each enemy you see on screen is an instance of that plan. Objects and instances explains the difference.
Sometimes two plans share most of what they do. A slime and a bat both have hit points. Both take damage. Both die at zero. Writing that twice is a waste. And if you fix a bug in one, you have to remember to fix the other.
A parent fixes this. Write the shared behavior once, in obj_enemy. Then make obj_slime and obj_bat children of obj_enemy. A child starts with everything its parent has. Then it adds things, or changes them.
You pick an object's parent in the Object editor. See The Code tab.
A parent can have a parent of its own. A slime can be a child of obj_enemy, and obj_enemy can be a child of obj_creature. Everything above an object in that chain is one of its ancestors. The nearest one is its parent. Next is its grandparent, and so on.
A child gets two things without writing anything.
obj_enemy makes hp in its Create event, then obj_slime has an hp too. The slime reads it and changes it like its own.obj_slime has no Step event, it runs obj_enemy's Step event. You do not copy anything.Here is a parent's Create event. It makes two variables.
// obj_enemy - Create event
hp = 10;
damage = 2;Now suppose obj_slime is a child of obj_enemy and has no Create event of its own. A new slime runs obj_enemy's Create. It starts with hp 10 and damage 2.
| Event | obj_enemy (parent) | obj_slime (child) | What a slime does |
|---|---|---|---|
| Create | sets hp and damage | none | runs the enemy's Create |
| Step | moves toward the player | none | runs the enemy's Step |
| Step | moves toward the player | bounces around | runs only the slime's Step |
The last row is the important one. The next section explains it.
The moment a child has its own version of an event, its parent's version of that event stops running for that child. The child's version replaces it. It does not add to it.
This is true even when the child's version does nothing. An empty Create event replaces the parent's Create. A Create event that holds only a comment replaces it too.
// obj_slime - Create event
// Nothing here yet. This still replaces obj_enemy's Create.This slime has no hp value. The variable exists, because the slime inherits it. But the code that gave hp its value was in obj_enemy's Create, and that code did not run. So hp starts at its empty value. For a number, that is 0.
The game gives no warning for this. An empty event is a real event. A common way to hit this is to add a new Create event to a child, only to type a comment in it, and then wonder where the parent's setup went.
Why work like this? A child has to be able to do something different from its parent. If every child event also ran the parent's, you could never change anything. So the rule is simple: your event, or the parent's. Never both, unless you ask for both.
You ask for both with event_call(parent).
It runs the parent's version of the event you are inside. Then your own code carries on. Put the call on the line where you want the parent's code to run.
// obj_slime - Create event
event_call(parent); // obj_enemy's Create runs here: hp = 10, damage = 2
bounce = 2; // then the slime's own setupNow a slime has hp, damage and bounce. The parent's work is done, and the slime added to it.
Where you put the call decides the order. The call above is first, so the parent's code runs first. Put it last, and the child's code runs first.
// obj_slime - Step event
hp += 1; // the slime heals a little first
event_call(parent); // then obj_enemy's Step runsevent_call(parent) always runs the same event you are in. Inside Create, it runs the parent's Create. Inside Step, it runs the parent's Step. You cannot use it to run a different event.
Sometimes there is no parent version to run. Then event_call(parent) does nothing. It is not an error and it gives no warning. The rest of your event runs as normal.
| Situation | What event_call(parent) does |
|---|---|
| The object has no parent | Nothing. |
| It has a parent, but neither the parent nor any ancestor writes this event | Nothing. |
| The parent writes this event | Runs the parent's version. |
| The parent does not write it, but the grandparent does | Runs the grandparent's version. |
The last row is the same rule as inheriting. The game looks up the chain for the nearest ancestor that writes the event, and uses that one.
// obj_rock - Create event. obj_rock has no parent.
event_call(parent); // there is nothing to run, so nothing happens
weight = 5;Doing nothing is on purpose. You can write the call in a child today and add a parent later. Or the parent can get a new event later. Either way, you do not have to change the child.
Each call goes up one step. Say obj_slime is a child of obj_enemy, and obj_enemy is a child of obj_creature. All three write a Step event, and the slime and the enemy each call event_call(parent).
The whole chain runs, and each object decides where in its own event the parent's part goes. If the enemy did not call event_call(parent), the creature's Step would not run for a slime. Each object has to ask for its own parent.
event_call(parent) works when you write it straight into an event of an object.
It does not work in a script. A script is not an event of any one object, so it has no parent. It does not work inside a with block either. Inside a with block, self is a different instance, and the parent's code would run on that one, not on the instance whose event this is. In both places the compiler stops you with a message.
A child gets what its parent has. It also works the other way round: the parent's name covers the whole family. A slime counts as an obj_enemy, and so does a bat.
with (obj_enemy) {
hp -= 1; // every slime, every bat, every plain enemy
}
if (instance_count(obj_enemy) > 5) {
hp = 0; // five or more enemies of any kind, so this one gives up
}The same goes for pos_meeting and the other overlap functions, instance_nearest, instance_furthest, instance_nth, collision_point, collision_rect, collision_circle and collision_line, and event_call(obj_enemy.user0). Each one looks at the parent's instances and at every child's. So does a Collision event: a hero's "Collision with obj_enemy" runs when the hero touches a slime or a bat too.
Use the parent's name when you mean every kind of enemy. Use a child's name when you mean only that kind: with (obj_slime) visits slimes and nothing else. It only goes down. A child's name never reaches its parent or its brothers and sisters.
Through the parent's name you can use what the parent has, which is its variables and its events. A variable that only obj_slime makes is out of reach there. If you need one, ask with with (obj_slime).
Overlap and collision lists every place this works. The one thing it does not do is instance_create, which always makes exactly the object you name.
The object's own settings. A parent's sprite, mask, and its visible, solid and persistent settings are not passed down. Set them on each child.